#ifndef __M_GUI_CTRL_H__
#define __M_GUI_CTRL_H__

#include <psp2/ctrl.h>

enum ExtCtrlButtons
{
    GUI_CTRL_BUTTON_UP = SCE_CTRL_UP,
    GUI_CTRL_BUTTON_RIGHT = SCE_CTRL_RIGHT,
    GUI_CTRL_BUTTON_DOWN = SCE_CTRL_DOWN,
    GUI_CTRL_BUTTON_LEFT = SCE_CTRL_LEFT,
    GUI_CTRL_BUTTON_A = SCE_CTRL_CROSS,
    GUI_CTRL_BUTTON_B = SCE_CTRL_CIRCLE,
    GUI_CTRL_BUTTON_X = SCE_CTRL_SQUARE,
    GUI_CTRL_BUTTON_Y = SCE_CTRL_TRIANGLE,
    GUI_CTRL_BUTTON_SELECT = SCE_CTRL_SELECT,
    GUI_CTRL_BUTTON_START = SCE_CTRL_START,
    GUI_CTRL_BUTTON_L1 = SCE_CTRL_L1,
    GUI_CTRL_BUTTON_R1 = SCE_CTRL_R1,
    GUI_CTRL_BUTTON_L2 = SCE_CTRL_L2,
    GUI_CTRL_BUTTON_R2 = SCE_CTRL_R2,
    GUI_CTRL_BUTTON_L3 = SCE_CTRL_L3,
    GUI_CTRL_BUTTON_R3 = SCE_CTRL_R3,
    GUI_CTRL_BUTTON_HOME = SCE_CTRL_PSBUTTON,                   // 1 << 16
    GUI_CTRL_BUTTON_LEFT_ANLOG_UP = SCE_CTRL_PSBUTTON << 1,     // 1 << 17
    GUI_CTRL_BUTTON_LEFT_ANLOG_RIGHT = SCE_CTRL_PSBUTTON << 2,  // 1 << 18
    GUI_CTRL_BUTTON_LEFT_ANLOG_DOWN = SCE_CTRL_PSBUTTON << 3,   // 1 << 19
    GUI_CTRL_BUTTON_LEFT_ANLOG_LEFT = SCE_CTRL_PSBUTTON << 4,   // 1 << 20
    GUI_CTRL_BUTTON_RIGHT_ANLOG_UP = SCE_CTRL_PSBUTTON << 5,    // 1 << 21
    GUI_CTRL_BUTTON_RIGHT_ANLOG_RIGHT = SCE_CTRL_PSBUTTON << 6, // 1 << 22
    GUI_CTRL_BUTTON_RIGHT_ANLOG_DOWN = SCE_CTRL_PSBUTTON << 7,  // 1 << 23
    GUI_CTRL_BUTTON_RIGHT_ANLOG_LEFT = SCE_CTRL_PSBUTTON << 8,  // 1 << 24
    GUI_CTRL_INTERCEPTED = SCE_CTRL_PSBUTTON << 9,              // 1 << 25
};

enum PadButtons
{
    PAD_UP,
    PAD_RIGHT,
    PAD_DOWN,
    PAD_LEFT,
    PAD_A,
    PAD_B,
    PAD_X,
    PAD_Y,
    PAD_SELECT,
    PAD_START,
    PAD_L1,
    PAD_R1,
    PAD_L2,
    PAD_R2,
    PAD_L3,
    PAD_R3,
    PAD_HOME,
    PAD_ENTER,
    PAD_CANCEL,
    PAD_LEFT_ANALOG_UP,
    PAD_LEFT_ANALOG_DOWN,
    PAD_LEFT_ANALOG_LEFT,
    PAD_LEFT_ANALOG_RIGHT,
    PAD_RIGHT_ANALOG_UP,
    PAD_RIGHT_ANALOG_DOWN,
    PAD_RIGHT_ANALOG_LEFT,
    PAD_RIGHT_ANALOG_RIGHT,
    N_PAD_BUTTONS,
};

#define ANALOG_CENTER 128
#define ANALOG_THRESHOLD 64
#define ANALOG_SENSITIVITY 16

#define N_CTRL_PORTS 4
#define LOCAL_CTRL_BUTTONS_BITMASK ((SCE_CTRL_PSBUTTON << 1) - 1)
#define LOCAL_CTRL_BUTTONS_BITMASK2 (GUI_CTRL_INTERCEPTED - 1)
#define DISABLE_PSBUTTON_HOLD_MICROS (500000llu) // 0.5 second (当按住home键到达这个时间时会禁用)

typedef uint8_t Pad[N_PAD_BUTTONS];

extern Pad old_pad, current_pad, pressed_pad, released_pad, hold_pad, hold2_pad;
extern Pad hold_count, hold2_count;

void GUI_ReadPad();
void GUI_CleanPad();

void GUI_SetControlEnabled(int enable); // 设置禁用后会一直禁用获取不到键值，需要手动去再开启
int GUI_IsControlEnabled();
void GUI_SetHomeKeyEnabled(int enable); // 只是内部传递，通过ReadPad仍能获取键值，接收判断为GUI_IsHomeKeyEnabled
int GUI_IsHomeKeyEnabled();

#endif